package com.nit;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

import lombok.extern.slf4j.Slf4j;
import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;

/**
 * Tess4jOcr测试示例
 *
 * @author liujh
 */
@Slf4j
public class Tess4jOcrTest {

    public static void main(String[] args) {

        Tess4jOcrTest test = new Tess4jOcrTest();
        test.ocrTest();

    }

    public void ocrTest() {

        log.info("ocrTest start....");
        long startMs = System.currentTimeMillis();

        //Tesseract的代码开始---------------------->>>>
        ITesseract instance = new Tesseract();

        /**
         * 组装接好tessdata目录的路径字符串
         */
//String filePathPre = System.getProperty("user.dir");
//    	String dataPath = filePathPre + File.separator + "tessdata";
        String dataPath = "D:\\opt\\img\\tessdata";

        /**
         * 设置目录datapath the tessdata path to set
         * 否则会报Please make sure the TESSDATA_PREFIX environment variable is set to your "tessdata" directory.错误
         */
        instance.setDatapath(dataPath);
//    	instance.setLanguage("eng");//默认，可以不写
        instance.setLanguage("chi_sim");//设置中文识别

        String imageName = "D:\\opt\\img\\f332f46a-6583-4861-bdc7-317c7b9a415d.jpg";
//String imageName="D:\\opt\\img\\35a269e8-a5b0-45a7-a05f-1927a028acda.jpg";
//        try (InputStream inStream = this.getClass().getResourceAsStream("/" + imageName)) {

        try (InputStream inStream = new FileInputStream(new File(imageName))) {
            BufferedImage bImage = ImageIO.read(inStream);
            //doOCR也可以传参为File，我这里传的BufferedImage
            String result = instance.doOCR(bImage);
            log.info("图片名：" + imageName + " 识别结果：" + result);
            //识别的结果回来可能会带回车，处理掉
            result = result.replaceAll("\n", "");
            result = result.replaceAll(" ", "");


            String ocrOrderStatus = "订单状态异常";
            if (result.contains("订单已完成")) {
                ocrOrderStatus = "订单状态正常";
            }
            String orderPrice = "订单金额异常";

            int potion = result.indexOf("合计");
            orderPrice = result.substring(potion + 3, potion + 10);


            log.info("ocr结果:ocrOrderStatus={},orderPrice={}", ocrOrderStatus, orderPrice);

        } catch (IOException e) {
            log.error(e.getMessage(), e);
        } catch (TesseractException e) {
            log.error(e.getMessage(), e);
        }


    }

}